<template>
  <PageDialog
    :title="title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    >
      <template v-slot:filter-searchKey>
        <el-input
          v-model="filterInfo.searchKey"
          :placeholder="placeholder"
          @change="changeStockSearchKey"
        />
      </template>
    </page-filter>
    <DialogTable
      ref="resourceTable"
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      v-model:query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :rowKey="rowKey"
      :highlight-row="highlightRow"
      :checkedRowList="checkedRowList"
      :reserve-selection="true"
      :check-box="checkBox"
      @pageInfo="pageInfo"
      @handleEvent="handleEvent"
    >
    </DialogTable>
  </PageDialog>
</template>
<script setup name="SelectProduct">
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  title: {
    type: String,
    default: '',
  },
  placeholder: {
    type: String,
    default: '',
  },
  highlightRow: {
    type: Boolean,
    default: false,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  api: {
    type: Function,
    default: undefined,
  },
  showPrice: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
  tableKey: {
    type: String,
    default: '',
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  queryKey: {
    type: String,
    default: 'keyword',
  },
  fieldList: {
    type: Array,
    default: function () {
      return [
        {
          label: '分析方案编码',
          value: 'planNo',
          minWidth: '100px',
        },
        {
          label: '分析方案名称',
          value: 'planName',
        },
        {
          label: '分析部门',
          value: 'deptName',
        },
        {
          label: '分析类型',
          value: 'analysisTypeValue',
        },
      ]
    },
  },
})
const clearType = ref(true)
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'create_time desc',
    ...props.query,
  },
  list: [
    {
      type: 'slot',
      value: 'searchKey',
    },
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  copyQueryMap: {},
  searchKey: props.query[props.queryKey],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: props.fieldList,
  pageInfo: {},
})
// 弹窗相关
const dialogInfo = ref({
  width: '70%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref([])
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  getList()
})
function changeStockSearchKey(val) {
  filterInfo.value.query[props.queryKey] = val
  search()
}
function clearSearch() {
  clearType.value = !clearType.value
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.query)
  filterInfo.value.searchKey = ''
  filterInfo.value.query[props.queryKey] = ''
  search()
}
// 获取列表
function getList() {
  proxy.$nextTick(() => {
    tableInfo.value.api = props.api
    tableInfo.value.refresh = Math.random()
  })
}
function search() {
  proxy.$nextTick(() => {
    tableInfo.value.api = props.api
    tableInfo.value.initCurpage = Math.random()
    tableInfo.value.refresh = Math.random()
  })
}
function pageInfo(obj) {
  tableInfo.value.pageInfo = obj
}
function save() {
  handleClose()
  if (!props.checkBox) {
    emits('callback', selectRow.value)
    return
  }
  selectRow.value.forEach((item) => {
    item.depotStockId = item.id
  })
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !(data && data.id)
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !data.length
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
</script>

<style scoped lang="scss">
.page-info {
  padding-top: 10px;
  span {
    padding-left: 10px;
  }
}
</style>
